<?php

namespace addons\kuerp\controller;

use addons\kuerp\model\GoodsSerial as GoodsSerialModel;
use app\admin\model\kuerp\Stores;
use think\Db;
use think\Exception;

/**
 * 产品序列号
 */
class GoodsSerial extends StaffApi {
	protected $noNeedLogin = [];
	protected $noNeedRight = [];


	//获取产品记录
	public function getList() {
		$limit  = input("limit/d", 10);
		$params = $this->request->post();
		$where  = [];
		$order  = 'id desc';
		$name  = input('name');
		if (!empty($name)) {//产品名称
			$where['goods_serial.serial|goods_serial.remark|goods_serial.goods_code|goods.goods_name'] = ['like', "%{$name}%"];
		}
		if (isset($params['goods_id']) && $params['goods_id']) {//产品id
			$where['goods_id'] = $params['goods_id'];
		}
		$storesList = (new Stores())->getStoresList(0, $this->auth->adminids);
		$storeids = array_column($storesList, 'id');
		if (isset($params['stores_id']) && $params['stores_id']) {//仓库
			$where['stores_id'] = $params['stores_id'];
		} else {
			if (isset($params['type']) && $params['type']) {//所属
				if ($params['type'] == 1) {//本人
					$where['stores_id'] = $this->auth->stores_info['id'] ?? 0;
				} elseif ($params['type'] == 2) {//下属
					//本人存在 则剔除自己
					if (!empty($this->auth->stores_info['id']) && in_array($this->auth->stores_info['id'], $storeids)) {
						$storeids = array_diff($storeids, [$this->auth->stores_info['id']]);
					}
					$where['stores_id'] = ['in', $storeids];
				}else{
					$where['stores_id'] = ['in', $storeids];
				}
			} else {
				$where['stores_id'] = ['in', $storeids];
			}
		}
		

		//在库状态
		if (!empty($params['store_status'])) {
			switch($params['store_status']) {
				case 1://在库
					$where['store_status'] = 1;
					$where['goods_serial.status'] = ['neq', 3];
					break;
				case 2://待出库
					$where['goods_serial.status'] = 3;
					$where['store_status'] = 1;
					break;
				case 3://已出库
					$where['store_status'] = 2;
					break;
			}
		}
		if (!empty($params['customer_id'])) {
			$salesids = Db::name('kuerp_goods_sales_info')->where(['customers_id'=>$params['customer_id'], 'stores_id' => $where['stores_id']])->column('id');
			$where['sale_id'] = ['in', $salesids];
		}


		$list              = GoodsSerialModel::with(['goods'])->where($where)->order($order)->paginate($limit);
		foreach ($list as $row) {
            $row->getRelation('goods')->visible(['goods_name', 'goods_image']);
			$row->visible(['id', 'goods_id', 'goods_code', 'stores_id', 'serial', 'status', 'store_status', 'remark', 'sale_id', 'sale_code', 'goods']);
        }
		// echo GoodsSerialModel::getLastSql();
		if ($list) {
			$list = $list->toArray();
			$default_img = cdnurl(config('site.default_goods_img'), true);
			$storeList = (new Stores())->getStoresList();
			$storeList = array_column($storeList,'name','id');
			//获取客户
			$sale_codes = array_column($list['data'],'sale_code');
			$customers = Db::name('kuerp_customers')->alias('c')->join('kuerp_goods_sales gs', 'gs.customers_id = c.id', 'left')->where(['gs.purchase_code'=>['in',$sale_codes]])->column('gs.id,customer_name','gs.purchase_code');
                
			$list['data'] = array_map(function ($v) use ($default_img, $storeList, $customers) {
				$v['goods_name'] = $v['goods']['goods_name'] ?? '';
				$v['goods_image'] = $v['goods']['goods_image'] ? cdnurl($v['goods']['goods_image'], true) : $default_img;
				$v['stores_name'] = $storeList[$v['stores_id']];
				$v['customer_name'] = $v['sale_code'] && !empty($customers[$v['sale_code']]) ? $customers[$v['sale_code']]['customer_name'] : '';
				$v['goods_salesid'] = $v['sale_code'] && !empty($customers[$v['sale_code']]) ? $customers[$v['sale_code']]['id'] : 0;
				return $v;
			}, $list['data']);
		}
		$list['stores'] = $storesList;
		$this->success('请求成功', $list);
	}


	//获取select产品列表
	public function getSelectList() {
		$name  = input('name');
		$limit  = input('limit', 20);
		$params = $this->request->post();
		$where = [
			'store_status' => 1
		];
		if ($name) {
			$where['serial'] = ['like', "%{$name}%"];
		}
        if (isset($params['goods_id']) && $params['goods_id']) {//产品id
			$where['goods_id'] = $params['goods_id'];
		}
        if (isset($params['stores_id']) && $params['stores_id']) {//仓库id
			$where['stores_id'] = $params['stores_id'];
		}
		if ($this->auth->is_sale && $this->auth->stores_info) {
			$where['stores_id'] = $this->auth->stores_info['id'];
		}

		$list = GoodsSerialModel::where($where)->field('id,goods_id,goods_code,stores_id,serial,status,store_status,remark,sale_id,sale_code')->order('id asc')->paginate($limit);
		$this->success('请求成功', $list);
	}


	//获取产品详情
	public function getProductDetail(){
	    $id=input('id');
	    $product=GoodsSerialModel::where(['id'=>$id])->find();
	    if(empty($product)){
	        $this->error('产品信息不存在');
        }
	    $this->success('请求成功',$product);
    }

	//获取select商品列表
    public function getGoodsList()
    {
        $limit = input("limit/d", 10);
        $name = input('name');
        $type_id = input('type_id');
        $where = [];
        if ($name) {
            $where['name|num'] = ['like', "%{$name}%"];
        }
        if ($type_id) {
            $where['type_id'] = $type_id;
        }
        $list = Goods::with(['type','specs'])->where($where)->paginate($limit);
        foreach ($list as $k=>$v) {
            $list[$k]['image'] = cdnurl($v['image'], true);

        }
        $this->success('请求成功', $list);
    }

}
